package com.activenetwork.smlite.testcase.sourcing;

import java.util.Calendar;
import java.util.List;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.XMLGregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.schema.model.event.EventType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.util.DateUtil;

public class RfpCaseEvent {
	static final Logger logger = LoggerFactory.getLogger(RfpCaseEvent.class);
	private EventType event;
	private EventBusiness eventBusiness = new EventBusiness();
	
	public RfpCaseEvent() throws DatatypeConfigurationException{
		// create an event
		event = new EventType();
		EventBasicType eventBasic = new EventBasicType();
		
		// event title
		eventBasic.setTitle("AutoTestRfpCaseEvent");
		
		// event location
		EventBasicType.Location location = new EventBasicType.Location();
		location.setLocationName("AutoTestRfpCaseEvent location");
		location.setAddress("AutoTestRfpCaseEvent address line 1");
		location.setAddressContinue("AutoTestRfpCaseEvent address line 2");
		location.setCityRegion("Shanghai");
		location.setStateProvince("SH");
		location.setPostalCode("200032");
		location.setCounty("AG");
		location.setPrivate(true);
		eventBasic.setLocation(location);
		
		//event category
		eventBasic.setCategory("Expo");
		
		// event length
		EventBasicType.Length length = new EventBasicType.Length();
		EventBasicType.Length.MultiDay multi = new EventBasicType.Length.MultiDay();
		length.setMultiDay(multi);
		eventBasic.setLength(length);
		
		//Start date
		Calendar startCal = Calendar.getInstance();
		XMLGregorianCalendar startDateCal = DateUtil.toXMLGregorianCalendar(startCal);
		multi.setStartDate(startDateCal);
		
		//end date
		DateUtil du = new DateUtil();
		XMLGregorianCalendar endDateCal = DateUtil.toXMLGregorianCalendar(du.calendarOffset(startCal, 3, true));
		multi.setEndDate(endDateCal);
		
		//Tag
		List<EventBasicType.Tag> tagList = eventBasic.getTag();
		EventBasicType.Tag tag = new EventBasicType.Tag();
		tag.setTagName("AutoTestRfpCaseEvent tag");
		tag.setTagDesc("AutoTestRfpCaseEvent desc");
		tagList.add(tag);
		
		//Component
		EventBasicType.Components components = new EventBasicType.Components();
		components.setSourcing(new Object());
		eventBasic.setComponents(components);
		

		event.setBasic(eventBasic);
	}
	
	public void createRfpCaseEvent(){
		String searchContent = event.getBasic().getTitle();
		try{
			logger.info("Make sure the specific event exist for rfp cases");
			eventBusiness.searchEvent(searchContent, event);
		}catch(RuntimeException e){
			logger.info("The event does not exist, we need to create it first for rfp cases");
			eventBusiness.addEvent(event);
		}
		
	}
	
	public EventType getEvent(){
		return event;
	}

}
